﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.woodsoft.core;
using EQ1.Elements;
using com.woodsoft.core.Files;
using System.Xml;
using EQ1.Objects;

namespace EQ1.Controls {
  public class LayoutBox : Control , IUIElement<LayoutBox> {
    public LayoutBox ( UIDocument doc , string name )
      : base( doc , name ) {
      this.Spacing = 5;
      this.ResizeLast = true;
      this.PaddingLeft = 0;
      this.PaddingRight = 0;
      this.PaddingTop = 0;
      this.PaddingBottom = 0;

    }
    public LayoutBox ( UIDocument doc )
      : this( doc , "LayoutBox" ) {
    }

    #region IUIElement<LayoutBox> Members

    public void Validate ( LayoutBox val , string name ) {
      UIClass par = ( UIClass ) this.ParentNode;

      if( val.LocalName != name ) {
        LayoutBox el = new LayoutBox( ( UIDocument ) val.OwnerDocument , name );
        el.Copy( val );
        val = el;
      }

      if( par.GetElement( name ) != null )
        par.RemoveElement( name );

      par.AppendChild( val );
    }

    public void Copy ( LayoutBox val ) {
      base.Copy( val );

      this.Spacing = val.Spacing;
      this.ResizeLast = val.ResizeLast;
      this.PaddingBottom = val.PaddingBottom;
      this.PaddingLeft = val.PaddingLeft;
      this.PaddingRight = val.PaddingRight;
      this.PaddingTop = val.PaddingTop;

      if( val.Pieces != null )
        this.Pieces = val.Pieces;
    }

    #endregion

    public int Spacing {
      get {
        return int.Parse( this.GetElement( "Spacing" ).InnerText );
      }
      set {
        this.Validate( "Spacing" , value.ToString() );
      }
    }
    public bool ResizeLast {
      get {
        return bool.Parse( this.GetElement( "ResizeLast" ).InnerText );
      }
      set {
        this.Validate( "ResizeLast" , value.ToString() );
      }
    }
    public int PaddingLeft {
      get {
        return int.Parse( this.GetElement( "PaddingLeft" ).InnerText );
      }
      set {
        this.Validate( "PaddingLeft" , value.ToString() );
      }
    }
    public int PaddingRight {
      get {
        return int.Parse( this.GetElement( "PaddingRight" ).InnerText );
      }
      set {
        this.Validate( "PaddingRight" , value.ToString() );
      }
    }
    public int PaddingTop {
      get {
        return int.Parse( this.GetElement( "PaddingTop" ).InnerText );
      }
      set {
        this.Validate( "PaddingTop" , value.ToString() );
      }
    }
    public int PaddingBottom {
      get {
        return int.Parse( this.GetElement( "PaddingBottom" ).InnerText );
      }
      set {
        this.Validate( "PaddingBottom" , value.ToString() );
      }
    }
    //Collection of ScreenPiece:item values
    public XmlNodeList Pieces {
      get {
        return this.GetElementsByTagName( "Pieces" );
      }
      set {
        foreach( XmlNode nd in value ) {
          if( nd.LocalName == "Pieces" ) {
            this.AddPiece( nd );
          }
        }
      }
    }

    public void AddPiece ( XmlNode val ) {
      if( !string.IsNullOrEmpty( val.InnerText ) ) {
        for( int i = this.Pieces.Count - 1 ; i >= 0 ; i-- ) {
          if( val.InnerText == this.Pieces[i].InnerText ) {
            return;
          }
        }

        if( val.LocalName == "Pieces" )
          this.AppendChild( val );
        else
          this.AddElement( "Pieces" , val.InnerText );
      }
    }
  }
}
